<template>
	<view class="geren">
		<u-navbar back-text="返回我的"></u-navbar>
		<u-form :model="form" ref="uForm" label-width="140" :error-type="errorType">
			<view class="content" style="margin-bottom: 134rpx;background: #fff;padding: 0 10px;">

				<u-form-item label="原密码" prop="password">
					<u-input v-model="form.password" placeholder="输入原密码" />
				</u-form-item>
				<u-form-item label="新密码" prop="newpassword1">
					<u-input v-model="form.newpassword1" placeholder="输入新密码" />
				</u-form-item>
				<u-form-item label="确认密码" prop="newpassword2">
					<u-input v-model="form.newpassword2" placeholder="确认密码" />
				</u-form-item>

			</view>
			<view class="footer safe-area-inset-bottom">
				<button style="background-color: #4a9add ;width: 50%;color: #fff;" @click="submit">确定</button>
			</view>

		</u-form>
	</view>
</template>

<script>
	import {
		UpdatePwd
	} from "@/pages/api/api"
	export default {
		data() {
			return {

				form: {
					password: '',
					newpassword1: '',
					newpassword2: '',
				},
				errorType: ['message'],
				rules: {

					password: [{
							required: true,
							message: '请输入原密码',
							trigger: ['change', 'blur'],
						},
						{
							min: 6,
							message: '密码最少6位数',
							trigger: ['change', 'blur'],
						},
						{
							pattern: /^[^\u4e00-\u9fa5]+$/,
							// 正则检验前先将值转为字符串
							transform(value) {
								return String(value);
							},
							message: '密码不能有中文'
						},
					],
					newpassword1: [{
							required: true,
							message: '请输入新密码',
							trigger: ['change', 'blur'],
						},
						{
							min: 6,
							message: '密码最少6位数',
							trigger: ['change', 'blur'],
						},
						{
							pattern: /^[^\u4e00-\u9fa5]+$/,
							// 正则检验前先将值转为字符串
							transform(value) {
								return String(value);
							},
							message: '密码不能有中文'
						},
					],
					newpassword2: [{
							required: true,
							message: '请输入确认密码',
							trigger: ['change', 'blur'],
						},
						{
							min: 6,
							message: '密码最少6位数',
							trigger: ['change', 'blur'],
						},
						{
							pattern: /^[^\u4e00-\u9fa5]+$/,
							// 正则检验前先将值转为字符串
							transform(value) {
								return String(value);
							},
							message: '密码不能有中文'
						},

						{
							// 自定义验证函数，见上说明 
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								if (this.form.newpassword1 != value) {
									callback(new Error('新密码和确认密码不一致'));
								} else {
									callback();
								}

							},
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					]
				}
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad() {


		},
		methods: {
			submit() {

				this.$refs.uForm.validate(valid => {
					if (valid) {
						UpdatePwd({
							oldPassword: this.form.password,
							newPassword: this.form.newpassword2,
						}).then(res => {
							console.log(111, res)
							if (res.code == 200) {
								uni.showToast({
									title: '密码修改成功',
									duration: 2000
								});
								setTimeout(() => {
									uni.navigateBack()
								}, 1500)
							}
						})

					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.footer {
		width: 100%;
		padding: 20rpx 0;
		position: fixed;
		bottom: 0;
		background: #fff;
		justify-content: center;
		display: flex;
		align-items: center;
	}
</style>
